package com.chinanet.repository.faultanticipate.manager;


import com.chinanet.entry.faultanticipate.manager.QRelation;
import com.chinanet.entry.faultanticipate.manager.Relation;
import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RestResource;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

public interface RelationRepository extends PagingAndSortingRepository<Relation, Long>, QuerydslBinderCustomizer<QRelation> ,QuerydslPredicateExecutor<Relation> {

    @RestResource(path = "root", rel = "root")
    @Query(value = "select * from relation where parent_id =0", nativeQuery = true)
    List<Relation> findAllByRoot();

    @RestResource(path = "all", rel = "all")
    @Query(value = "select * from relation", nativeQuery = true)
    List<Relation> findAll();

    @RestResource(path = "parent", rel = "parent")
    List<Relation> findAllByParentId(@Param("parentId") int parentId);

    @RestResource(path = "name", rel = "name")
    @Query(value = "select * from relation where name =:name", nativeQuery = true)
    Relation findByName(@Param("name") String name);

    @RestResource(path = "tparent", rel = "tparent")
    @Query(value = "select * from relation a left join relation b on a.parent_id=b.id  where a.name=:name and b.name=:tname", nativeQuery = true)
    Relation findByTName(@Param("name") String name, @Param("tname") String tname);

    @RestResource(path = "tparent2", rel = "tparent2")
    @Query(value = "select * from relation s,relation n where s.name=:name and n.name=:name and s.parent_id=n.id and n.name = :tname", nativeQuery = true)
    Relation findByName(@Param("name") String name, @Param("tname") String tname);

    @RestResource(path = "tname", rel = "tname")
    @Query(value = "select a.id from relation a,relation b  where b.name =:name and a.parent_id=b.id", nativeQuery = true)
    List<Relation> findAllSonBig(@Param("name") String name);


    // 过滤
    default void customize(QuerydslBindings bindings, QRelation relation) {
/*        bindings.bind(relation.name).first((path, value) -> path.contains(value));
        bindings.bind(relation.name).first((path, value) -> path.containsIgnoreCase(value));*/
        bindings.bind(String.class).first((StringPath path, String value) -> {
            // 中文乱码解决问题
            try {
                String newValue=URLDecoder.decode(value,"UTF-8");
                System.out.println(newValue);
                return  path.contains(newValue);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return  null;
            }
        });
    }
}